<?php require_once(dirname(__FILE__).'/../include/config.inc.php');

require_once "function.php";
/*
进度：
0 已经撤单
1 报修成功
2 客服已经联系
3 系统已经派单
4 工程师接单
5 维修完成
6 已经评价
*/

// 会员信息
$uid=$_SESSION['uid'] ;
$orderid= empty($_GET['orderid']) ? '' : htmlspecialchars($_GET['orderid']); //进度状态


if($orderid==""){
	exit();	
}

//判断是否评价
$row = $dosql->GetOne("SELECT * FROM `#@__order_evaluate` WHERE orderid='$orderid' ");
if(is_array($row) && isset($row['id'])){
		// 返回用户 id 
		$pingjia=$row;
		$has_eval=true;
		$start=$row['start'];
		$remark=$row['remark'];
		$start_str="";
		for($i=0;$i<$start;$i++){
			$start_str.='<img src="images/x2.png">';
		}
}else{
		$has_eval=false;
}




?>

<!DOCTYPE html>
<html>
<head>
<title><?php echo $cfg_webname;?> </title>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
<meta name="description" content="<?php echo $cfg_webname;?>">
<link rel="stylesheet" href="lib/weui.min.css">
<link rel="stylesheet" href="css/jquery-weui.css">
<link rel="stylesheet" href="css/font-awesome.min.css">
<link rel="stylesheet" href="css/demos.css">
<script src="lib/jquery-2.1.4.js"></script> 
<script src="lib/fastclick.js"></script> 
<script>
  $(function() {
    FastClick.attach(document.body);
  });
</script> 
<script src="js/jquery-weui.js"></script> 

  <style>

    .iconfont {
      font-family:"iconfont" !important;
      font-size:16px;
      font-style:normal;
      -webkit-font-smoothing: antialiased;
      -webkit-text-stroke-width: 0.2px;
      -moz-osx-font-smoothing: grayscale;
    }
    body{
      font-family: "microsoft yahei";
    }
    ul,li{
      list-style: none;
      padding:0;
      margin:0;
    }
    a{
      text-decoration: none;
    }
    .clearfix:after {
      content: ".";
      display: block;
      height: 0;
      clear: both;
      visibility: hidden;
    }
    .order-evaluation {
      background: #fff;
      z-index: 200;
    }
    .order-evaluation{
      padding:0 22px;
    }
    .order-evaluation h4{
      font-size:18px;
      color:#333333;
      padding-bottom:20px;
      border-bottom:1px dashed #dbdbdb;
    }
    .order-evaluation p{
      font-size:14px;
      color:#999;
      line-height:25px;
      margin-bottom:10px;
    }
    .order-evaluation .order-evaluation-text{
      font-size:16px;
      color:#333;
      line-height:40px;
      padding-left:15px;
      background:#f3f3f3;
      margin-bottom:15px;
      margin-top:10px;
    }
    .order-evaluation-checkbox ul li{
      width:100px;
      height:43px;
      border:1px solid #e8e8e8;
      text-align: center;
      background: #fff;
      font-size:14px;
      color:#333333;
      line-height:43px;
      margin-right:10px;
      margin-bottom:10px;
      float:left;
      cursor: pointer;
      overflow: hidden;
      position:relative;
    }
    .order-evaluation-checkbox ul li.checked i{
      display: block;
    }
    .order-evaluation-checkbox ul li.checked{
      border:1px solid #e84c3d;
    }
    .order-evaluation .order-evaluation-textarea{
      position:relative;
      height: 120px;
    }
    .order-evaluation .order-evaluation-textarea textarea{
      height:auto;
      border:1px solid #e8e8e8;
      position:absolute;
      top:0;
      left:0;
      line-height:22px;
      padding:15px;
      color:#666; width:90%;
    }
    .order-evaluation .order-evaluation-textarea span{
      position:absolute;
      bottom:10px;
      font-size:12px;
      color:#999;
      right:10px;
    }
    .order-evaluation .order-evaluation-textarea span em{
      color:#e84c3d;
    }
    .order-evaluation>a{
		 width:120px;
      height:48px;
      border-radius: 6px;
      display: block;
      text-align: center;
      line-height:48px;
      background:#f36a5a;
      float:right;
      margin-top:20px;
      color:#fff;
      font-size:14px;
	  margin-bottom:30px;
    }
    .order-evaluation-checkbox ul li i {
      display: none;
      color: #e84c3d;
      position: absolute;
      right: -4px;
      bottom: -14px;
      font-size: 20px;
    }
    .order-evaluation>a:hover{
      background: #e84c3d;
    }
    .block li label,.con span{ font-size: 18px; margin-right: 20px; line-height: 23px;}
    .block li span{display: inline-block; vertical-align: middle; cursor: pointer;}
    .block li span img{margin-right: -5px; }
    .level{color:#e84c3d;font-size:16px;margin-left:15px;position: relative;top: 3px;}
    .dmlei_tishi_info{
      height:70px;border-radius: 10px;background: rgba(0,0,0,0.5);font-size:18px;color:#fff;text-align: center;line-height: 70px;position:fixed;
      left: 48%;
      display: none;
      margin-left: -128px;
      top: 40%;
      margin-top: -35px;
      padding: 0 15px;
      z-index: 1000;
    }
  </style>
</head>
<body ontouchstart>
<div class="top"> <img src="images/top.jpg" width="100%"> </div>
<div class="main" >
  <div class="weui-cells__title"><i class="fa fa-id-card-o" aria-hidden="true"></i>工单评价</div>
<?php if($has_eval) {?>
<div class="order-evaluation clearfix">

  <p>本次评价的星级：</p>
  <div class="block">
    <ul>
      <li data-default-index="0">
            <span>
				<?php echo $start_str;?>
            </span>
        <em class="level"><?php echo $start;?>星</em>
      </li>
    </ul>
  </div>
  <div class="order-evaluation-text">
    您对本次服务的印象：
  </div>
 
  <div class="order-evaluation-textarea">
    <?php echo $remark;?>
    
  </div>
  <a href="javascript:history.go(-1);" rel="external nofollow" > 返回</a>
</div>

</div>
<?php }else{
	
	$order_row= $dosql->GetOne("SELECT * FROM `#@__order`   WHERE  id='$orderid'   "); 
	if(is_array($order_row) && isset($order_row['id'])){
		
		if($order_row['jindu']<'5'){
		echo '<script type="text/javascript">$.toast("没有完成的任务不能评价", function() {
			 window.location.href="orderDetail.php?orderid='.$orderid.'";
			});
		</script>';
			exit();
		}
		if($order_row['uid']!=$uid){
		echo '<script type="text/javascript">$.toast("只能评价属于你的单子哦！", function() {
			 window.location.href="orderDetail.php?orderid='.$orderid.'";
			});
		</script>';
			exit();
		}
	
	?>
<div class="order-evaluation clearfix">
  <div class="weui-cells__title">给“
        <?php 
			if(!empty($order_row['manageid'])){
				$m_str=$order_row['manageid'];
				$m_arr=explode(",",$m_str);
				foreach($m_arr as $key=>$val){
					echo ' <i class="fa  fa-user-o red"></i>      '.getManage($val);
					
				}
			}
		?>
  
  ”的评价</div>
  <p>请对服务做出评价！最高5星，最低1星</p>
  <div class="block">
    <ul>
      <li data-default-index="0">
            <span>
              <img src="images/x1.png">
              <img src="images/x1.png">
              <img src="images/x1.png">
              <img src="images/x1.png">
              <img src="images/x1.png">
            </span>
        <em class="level"></em>
      </li>
    </ul>
  </div>
  <div class="order-evaluation-text">
    本次服务， 给您留下了什么印象呢？
  </div>
 
  <div class="order-evaluation-textarea">
   <form name="form" id="form" method="post" action="">
        <textarea name="remark" id="remark" onkeyup="words_deal();"  placeholder="请输入印象"></textarea>
        <input type="hidden" name="start" id="start" value="">
        <input type="hidden" name="action" id="action" value="add" />
        <input type="hidden" name="uid" id="uid" value="<?php echo $uid?>" />
        <input type="hidden" name="orderid" id="orderid" value="<?php echo $orderid?>" />
   </form>
    <span>还可以输入<em id="textCount">140</em>个字</span>
  </div>
  <a href="javascript:;" rel="external nofollow" id="order_evaluation">立即评价</a>
</div>
<div id="order_evaluate_modal" class="dmlei_tishi_info"></div>
</div>
<?php 
	}
} ?>
</body>
<script>
  /*
   * 根据index获取 str
   * **/
  function byIndexLeve(index){
    var str ="";
    switch (index)
    {
      case 0:
        str="1星";
        break;
      case 1:
        str="2星";
        break;
      case 2:
        str="3星";
        break;
      case 3:
        str="4星";
        break;
      case 4:
        str="5星";
        break;
    }
    return str;
  }
  // 星星数量
  var stars = [
['x2.png', 'x1.png', 'x1.png', 'x1.png', 'x1.png'],
    ['x2.png', 'x2.png', 'x1.png', 'x1.png', 'x1.png'],
    ['x2.png', 'x2.png', 'x2.png', 'x1.png', 'x1.png'],
    ['x2.png', 'x2.png', 'x2.png', 'x2.png', 'x1.png'],
    ['x2.png', 'x2.png', 'x2.png', 'x2.png', 'x2.png'],
  ];
  $(".block li").find("img").hover(function(e) {
    var obj = $(this);
    var index = obj.index();
    if(index < (parseInt($(".block li").attr("data-default-index")) -1)){
      return ;
    }
    var li = obj.closest("li");
    var star_area_index = li.index();
    for (var i = 0; i < 5; i++) {
      li.find("img").eq(i).attr("src", "images/" + stars[index][i]);//切换每个星星
    }
	
    $(".level").html(byIndexLeve(index));
  }, function() {
  })
  $(".block li").hover(function(e) {
  }, function() {
    var index = $(this).attr("data-default-index");//点击后的索引
    index = parseInt(index);
    console.log("index",index);
    $(".level").html(byIndexLeve(index-1));
    console.log(byIndexLeve(index-1));
    $(".order-evaluation ul li:eq(0)").find("img").attr("src","images/x1.png");
    for (var i=0;i<index;i++){
      $(".order-evaluation ul li:eq(0)").find("img").eq(i).attr("src","images/x2.png");
    }
  })
  $(".block li").find("img").click(function() {
    var obj = $(this);
    var li = obj.closest("li");
    var star_area_index = li.index();
    var index1 = obj.index();
    li.attr("data-default-index", (parseInt(index1)+1));
    var index = $(".block li").attr("data-default-index");//点击后的索引
    index = parseInt(index);
	$("#start").val(index);
    console.log("index",index);
    $(".level").html(byIndexLeve(index-1));
    console.log(byIndexLeve(index-1));
    $(".order-evaluation ul li:eq(0)").find("img").attr("src","images/x1.png");
    for (var i=0;i<index;i++){
      $(".order-evaluation ul li:eq(0)").find("img").eq(i).attr("src","images/x2.png");
    }
  });

  //评价字数限制
  function words_deal()
  {
    var curLength=$("#remark").val().length;
    if(curLength>140)
    {
      var num=$("#remark").val().substr(0,140);
      $("#remark").val(num);
      alert("超过字数限制，多出的字将被截断！" );
    }
    else
    {
      $("#textCount").text(140-$("#remark").val().length);
    }
  }
  $("#order_evaluation").click(function(){
	  var start=$("#start").val();
	  var remark=$("#remark").val();
	  if(start==""){
			$.toast("请选择星级","forbidden");
			return false;
	  }
	  $("#form").submit();
   
  })
</script>

<?php


if(isset($action) and $action=='add'){
	if( empty($start) or  empty($orderid) or empty($uid) )
	{
	echo '<script type="text/javascript">
	$.toast("操作不正确", function() {
		 window.location.href="orderDetail.php?orderid='.$orderid.'";
		});
	</script>';
		exit();
	}
	

	
	//如果是新客户，保存收货地址


	if(!$has_eval){
		$start				=htmlspecialchars($start);
		$remark			=htmlspecialchars($remark);
		$uid					=htmlspecialchars($uid);
		$orderid			=htmlspecialchars($orderid);
		$post_time		=time();
		$post_time2	=date("Y-m-d H:i:s",time());
	
	//更新工单状态
	$dosql->ExecNoneQuery("update `#@__order` set  jindu='6'  where id='$orderid '  ");

	//新建工单评价
	$sql2="insert into `#@__order_evaluate`(orderid,openid,uid,post_time,remark,start) values ('$orderid','$openid','$uid','$post_time','$remark','$start')";
	$dosql->ExecNoneQuery($sql2);
	//插入更新的状态
	$dosql->ExecNoneQuery("insert into `#@__order_log`(orderid,op,op_time,jindu) values ('$orderid','用户评价','$post_time','6')");
	
	//5星好评 增加积分
	if($start=='5'){
		foreach($m_arr as $key=>$val){
				//增加奖励计划 
			$dosql->ExecNoneQuery("update  `#@__user` set fee=fee+'1' where id='$val' ");
			//写入 奖励操作记录
			$dosql->ExecNoneQuery("insert into `#@__fee_log`(orderid,op,op_time,jindu,manageid,remark,fee) values ('$orderid','add','$post_time','6','$val','5星好评 奖励1元','1')");
		}
	}
	
	$orderidname=date('Ymd',time())."-".$orderid;
	//封装为json格式的数据
	/*
	$json='{
	  "touser": "'.$openid.'" ,  
	  "template_id": "h5lexxPeUmIA5oAwBMtnIELTO27RC7OugnX7RuCpQJY",    
	  "url":"https://www.hanzhibo.cn/wx/order.php",      
	  "data": {
		  "first": {
			  "value": "您好，报修成功，即将派单维修", 
			  "color": "#f71b03"
		  }, 
		  "keyword1": {
			  "value": "'.$orderidname.'", 
			  "color": "#173177"
		  }, 
		  "keyword2": {
			  "value": "'.$post_time2.'", 
			  "color": "#173177"
		  },
		  "keyword3": {
			  "value": "'.$apply_device.'", 
			  "color": "#0e48cb"
		  },
		  "keyword4": {
			  "value": " '.$json_address.'", 
			  "color": "#173177"
		  },
		  "keyword5": {
			  "value": "'.$content.'", 
			  "color": "#173177"
		  },
		  "remark": {
			  "value": "系统正在派单，请稍等。马上会有工程师和您联系，请保持通讯畅通。售后热线：400-888-5607", 
			  "color": "#f71b03"
		  } 
	  }
	}';
	sendWxMsg($json);
	*/
	echo '<script type="text/javascript">
	$.toast("评价成功", function() {
		 window.location.href="order.php";
		});
	</script>';
	exit();
	}
}

?>


</html>